package com.example.administrator.matriximage.viewpack;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

public abstract class EventImage extends ImageView implements IEventPost {


    public EventImage(Context context) {
        super(context);
    }

    public EventImage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public EventImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public EventImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }



    final String tag=getClass().getName();

    ScaleGestureDetector.SimpleOnScaleGestureListener simpleOnScaleGestureListener=new ScaleGestureDetector.SimpleOnScaleGestureListener()
    {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {

            iEventPost.scaleBeginEvent(detector);
            return super.onScale(detector);
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {

            iEventPost.scaleEvent(detector);
            return super.onScaleBegin(detector);
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            iEventPost.scaleEndEvent(detector);
            super.onScaleEnd(detector);
        }
    };
    ScaleGestureDetector scaleGestureDetector=new ScaleGestureDetector(getContext(),simpleOnScaleGestureListener);




    private IEventPost iEventPost=this;





    /*没有别的事情，只处理事件*/
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(iEventPost!=null)
        {

            if(event.getPointerCount()==1)/*如果是单手操作*/
            {
                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                    {

                        iEventPost.downEvent(event);
                    }
                    break;
                    case MotionEvent.ACTION_MOVE:
                    {
                        iEventPost.moveEvent(event);
                    }
                    break;
                    case MotionEvent.ACTION_UP:
                    {
                        iEventPost.upEvent(event);
                    }
                    break;
                }
            }else if(event.getPointerCount()==2)/*如果是双手操作*/
            {
                scaleGestureDetector.onTouchEvent(event);
            }




        }


        return super.onTouchEvent(event);
    }






}
